package com.zyh.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class CheckUserLoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
//        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
//        try {
//            final String uri = httpServletRequest.getRequestURI().startsWith("/") ? httpServletRequest.getRequestURI().substring(1) : httpServletRequest.getRequestURI();
//            if(uri.contains("/login.jsp") || uri.contains("/user/checkLogin")){
//                filterChain.doFilter(httpServletRequest, servletResponse);
//                return;
//            }
//        } catch (Exception e) {
//            filterChain.doFilter(httpServletRequest,httpServletResponse);
//        }
//
//        if(httpServletRequest.getSession().getAttribute("user")!=null){
//            filterChain.doFilter(httpServletRequest,httpServletResponse);
//        }else{
//            httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/login.jsp");
//        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
